
// import Area
import { Protocol } from "./protocol"
import { Sender } from "./sender"

const app = getApp()
const sender = new Sender()

/**
 * 蓝牙解绑设备 - 暂只支持全部解绑
 */
export function decodeAllDev() {
    let params = new Uint8Array(9)
    params[0] = 0x00
    // mac1 - dev
    params[3] = 0xFF
    params[4] = 0xFF
    params[5] = 0xFF
    // mac2 - room
    params[6] = 0xFF
    params[7] = 0xFF
    params[8] = 0xFF
    sender.sendData(Protocol.encode(0x02, 0x00FFFFFF, params))
}

/**
 * 蓝牙配对设备
 * @param {Number} devMac 
 * @param {Number} roomMac 
 */
export function pairDev(devMac, roomMac) {
    let params = new Uint8Array(9)
    params[0] = 0x01
    // mac1 - dev
    params[3] = (devMac & 0x00ff0000) >> 16
    params[4] = (devMac & 0x0000ff00) >> 8
    params[5] = devMac & 0xff
    // mac2 - room
    params[6] = (roomMac & 0xff0000) >> 16
    params[7] = (roomMac & 0xff00) >> 8
    params[8] = roomMac & 0xff
    sender.sendData(Protocol.encode(0x02, devMac, params))
}

/**
 * 仅开灯，保留原色温和亮度
 * @param {Number} mac 
 */
export function turnOn(mac) {
    let params = new Uint8Array(9)
    params[0] = 0x23
    sender.sendData(Protocol.encode(0x3B, mac, params))
}

/**
 * 仅关灯，保留原色温和亮度
 * @param {Number} mac 
 */
export function turnOff(mac) {
    let params = new Uint8Array(9)
    params[0] = 0x24
    sender.sendData(Protocol.encode(0x3B, mac, params))
}

/**
 * 单独修改亮度，保持原色温
 * @param {Number} brightness 0-100，对应亮度0x00~0x64
 */
export function setBrightness(mac, brightness) {
    let params = new Uint8Array(9)
    params[0] = 0x01
    params[5] = brightness // 设置亮度0x00~0x64 等于10进制的0~100（js自动转换）
    params[7] = 0x03
    sender.sendData(Protocol.encode(0x3B, mac, params))
}

/**
 * (!存在问题）单独调整色温，保持原亮度值
 * @param {Number} cct 0-100, 对应色温0x00~0x64
 * @param {Number} mac 
 */
export function setCCT(mac, cct) {
    let params = new Uint8Array(9)
    params[0] = 0xB2
    params[4] = cct // 设置色温0x00~0x64 等于10进制的0~100（js自动转换）
    // params[5] = brightness // 设置亮度0x00~0x64 等于10进制的0~100（js自动转换）
    params[7] = 0x03
    sender.sendData(Protocol.encode(0x3B, mac, params))
}

/**
 * 控制色温和亮度
 * @param {Number} cct 
 * @param {Number} brightness 
 * @param {Number} mac 
 */
export function setFull(mac, cct, brightness) {
    let params = new Uint8Array(9)
    params[0] = 0xB1
    params[4] = cct // 设置色温0x00~0x64 等于10进制的0~100（js自动转换）
    params[5] = brightness // 设置亮度0x00~0x64 等于10进制的0~100（js自动转换）
    params[7] = 0x03
    sender.sendData(Protocol.encode(0x3B, mac, params))
}

/**
 * 渐变关闭，最大支持6553.5秒，即1.8小时
 * @param {Number} mills <= 6553.5 毫秒
 * @param {Number} mac 
 */
export function offDelayer(mac, mills) {
    let params = new Uint8Array(9)
    params[0] = 0x75
    params[6] = (mills & 0xff00) >> 8
    params[7] = mills & 0x00ff
    sender.sendData(Protocol.encode(0x3B, mac, params))
}

/**
 * 定时关闭，最大支持65535秒，即18小时
 * @param {Number} seconds <= 65535 秒
 * @param {Number} mac 
 */
export function offTimer(mac, seconds) {
    let params = new Uint8Array(9)
    params[0] = 0x73
    params[6] = (seconds & 0xff00) >> 8
    params[7] = seconds & 0x00ff
    sender.sendData(Protocol.encode(0x3B, mac, params))
}

/**
 * 设置 homeList 本地缓存
 */
function initRoomCache() {
    // console.log("wx.getStorageSync('roomCache'):", wx.getStorageSync('roomCache'))
    if (wx.getStorageSync('roomCache')) {
        // 存在roomCache，直接退出
        console.log("initRoomCache fail, its already exit ", wx.getStorageSync('roomCache'))
        return
    } else {
        // 不存在roomCache
        var list = app.globalData.homeList
        var tmp = []
        var roomClass = { roomId: 0, roomBrightness: 0, roomPreset: 0x00, dev: [] }
        var devClass = { devId: 0, devBrightness: 0, devPreset: 0x00 }
        for (let i = 0; i < list.length; i++) {
            roomClass.roomId = list[i].roomId
            var light = []
            for (let j = 0; j < list[i].devList.length; j++) {
                var b = JSON.parse(list[i].devList[j].deviceBrightness)
                light.push(b)
                devClass.devId = list[i].devList[j].deviceId
                devClass.devBrightness = JSON.parse(list[i].devList[j].deviceBrightness)
                roomClass.dev.push(devClass)
                devClass = { devId: 0, devBrightness: 0, devPreset: 0x00 }
            }

            if (light.length) {
                roomClass.roomBrightness = Math.max.apply(null, light)
            } else {
                roomClass.roomBrightness = 0
            }
            tmp.push(roomClass)
            roomClass = { roomId: 0, roomBrightness: 0, roomPreset: 0x00, dev: [] }
        }
        // console.log("local cache --- ", tmp)
        wx.setStorage({
            key: 'roomCache',
            data: tmp,
            success(res) {
                console.log("initRoomCache success : ", tmp)
            }
        })
    }

}

/**
 * 初始化 preset 本地缓存
 */
export function initPresetActiveCache() {

    if (wx.getStorageSync('presetActive')) {
        // 存在preset cache，直接退出
        console.log("initPresetCache already exit", wx.getStorageSync('preset'))
        return
    } else {
        // 不存在preset cache , 执行initPresetCache
        var list = app.globalData.homeList
        var roomP = []
        var devP = []
        var roomPreset = { roomId: 0, preset: 0, dev: [] }
        var devPreset = { devId: 0, preset: 0 }
        for (let i = 0; i < list.length; i++) {
            roomPreset.roomId = list[i].roomId
            for (let j = 0; j < list[i].devList.length; j++) {
                devPreset.devId = list[i].devList[j].devId
                devP.push(devPreset)
            }
            roomPreset.dev = devP
            roomP.push(roomPreset)
            // clear
            roomPreset = { roomId: 0, preset: 0, dev: [] }
            devPreset = { devId: 0, preset: 0 }
            devP = []
        }

        wx.setStorage({
            key: 'presetActive',
            data: roomP,
            success(res) {
                console.log("setPresetCache success ", res)
            },
            fail(res) {
                console.log("setPresetCache fail ", res)
            }
        })
    }
}

/**
 * 修改 preset 本地缓存
 * @param {Number} roomId 
 * @param {Number} devId canbe null
 * @param {Number} status 
 */
export function setPresetCache(roomId, devId, status) {
    list = wx.getStorageSync('presetActive')
    if (devId = null) {
        // set room & dev
        for (let i = 0; i < list.length; i++) {
            if (roomId == list[i].roomId) {

            }
        }

    } else {
        // set dev

    }
}